<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>adapter模式 &mdash; programming v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="top" title="programming v0.1 documentation" href="../index.html" />
    <link rel="up" title="设计模式学习笔记" href="index.html" />
    <link rel="next" title="facade模式" href="facade.html" />
    <link rel="prev" title="command模式" href="command.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="facade.html" title="facade模式"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="command.html" title="command模式"
             accesskey="P">上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" accesskey="U">设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="adapter">
<h1>adapter模式<a class="headerlink" href="#adapter" title="永久链接至标题">¶</a></h1>
<div class="section" id="id1">
<span id="index-1"></span><span id="id2"></span><h2>adapter模式定义<a class="headerlink" href="#id1" title="永久链接至标题">¶</a></h2>
<p><strong>Adapter Pattern</strong> converts the interface of a class into another interface the clients expect.
Adapter lets classes work together that couldn&#8217;t otherwise because of incompatible interfaces.</p>
<p><strong>适配器模式</strong> 将一个类的接口转换为 <strong>客户期待</strong> 的另一个接口.
适配器使得本来由于接口不匹配而不能工作在一起的类可以工作在一起.</p>
</div>
<div class="section" id="id3">
<h2>问题描述<a class="headerlink" href="#id3" title="永久链接至标题">¶</a></h2>
<p>鸭子和火鸡两个不同接口的类, 由于用户只知道鸭子类,所以当用户需要使用相同的接口来调用火鸡类时,
需要 <strong>适配器模式</strong> 来进行处理.</p>
<p>适配器模式的类图如下:</p>
<img alt="../_images/adapter_class.png" src="../_images/adapter_class.png" />
<p>其中基于对象的 <strong>适配器模式</strong> 采用 <strong>组合</strong> 的方式,
而基于类的 <strong>适配器模式</strong> 采用 <strong>继承</strong> 的方式(多重继承).</p>
</div>
<div class="section" id="id4">
<h2>具体代码<a class="headerlink" href="#id4" title="永久链接至标题">¶</a></h2>
<p>下载请点击 <a href="../_downloads/adapter.cpp"><strong class="xref">这里</strong></a></p>
<div class="highlight-c++"><table class="highlighttable"><tr><td class="linenos"><pre> 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82</pre></td><td class="code"><div class="highlight"><pre><span class="cp">#include &lt;iostream&gt;</span>
<span class="cp">#include &lt;string&gt;</span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>

<span class="k">class</span> <span class="nc">Duck</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
       <span class="k">virtual</span> <span class="kt">void</span> <span class="n">quack</span><span class="p">(){}</span>
       <span class="k">virtual</span> <span class="kt">void</span> <span class="n">fly</span><span class="p">(){}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">Turkey</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">gobble</span><span class="p">(){}</span>
        <span class="k">virtual</span> <span class="kt">void</span> <span class="n">fly</span><span class="p">(){}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">MallardDuck</span><span class="o">:</span><span class="k">public</span> <span class="n">Duck</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="kt">void</span> <span class="n">quack</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Quack&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>  <span class="c">// the concrete class</span>
        <span class="kt">void</span> <span class="n">fly</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;I&#39;m flying&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">WildTurkey</span><span class="o">:</span><span class="k">public</span> <span class="n">Turkey</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="kt">void</span> <span class="n">gobble</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;Gobble gobble&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
        <span class="kt">void</span> <span class="n">fly</span><span class="p">(){</span><span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;I&#39;m flying a short distance.&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">DuckAdapter</span><span class="o">:</span><span class="k">public</span> <span class="n">Duck</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="n">Turkey</span> <span class="o">*</span><span class="n">turkey</span><span class="p">;</span>
        <span class="n">DuckAdapter</span><span class="p">(</span><span class="n">Turkey</span> <span class="o">*</span><span class="n">turkey</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">this</span><span class="o">-&gt;</span><span class="n">turkey</span> <span class="o">=</span> <span class="n">turkey</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="kt">void</span> <span class="n">quack</span><span class="p">(){</span><span class="n">turkey</span><span class="o">-&gt;</span><span class="n">gobble</span><span class="p">();}</span>
        <span class="kt">void</span> <span class="n">fly</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="mi">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>  <span class="c">// turkey cannot fly very long, so fly 5 times to get the duck&#39;s distance</span>
                <span class="n">turkey</span><span class="o">-&gt;</span><span class="n">fly</span><span class="p">();</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="k">class</span> <span class="nc">DuckAdapter_MI</span><span class="o">:</span><span class="k">public</span> <span class="n">Duck</span><span class="p">,</span> <span class="k">public</span> <span class="n">WildTurkey</span>     <span class="c">// using Multi-Inheritance</span>
<span class="p">{</span>
    <span class="k">public</span><span class="o">:</span>
        <span class="kt">void</span> <span class="n">quack</span><span class="p">(){</span><span class="n">gobble</span><span class="p">();}</span>
        <span class="kt">void</span> <span class="n">fly</span><span class="p">()</span>
        <span class="p">{</span>
            <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">&lt;</span><span class="mi">5</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
                <span class="n">WildTurkey</span><span class="o">::</span><span class="n">fly</span><span class="p">();</span>
        <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="n">main</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;############# The real duck start #####################&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">MallardDuck</span> <span class="n">duck</span><span class="p">;</span>
    <span class="n">duck</span><span class="p">.</span><span class="n">quack</span><span class="p">();</span>
    <span class="n">duck</span><span class="p">.</span><span class="n">fly</span><span class="p">();</span>

    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;############# The real turkey start #####################&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">WildTurkey</span> <span class="n">turkey</span><span class="p">;</span>
    <span class="n">turkey</span><span class="p">.</span><span class="n">gobble</span><span class="p">();</span>
    <span class="n">turkey</span><span class="p">.</span><span class="n">fly</span><span class="p">();</span>

    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;############# The fake duck(turkey) start based on composition #####################&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">DuckAdapter</span> <span class="n">fakeDuck</span><span class="p">(</span><span class="o">&amp;</span><span class="n">turkey</span><span class="p">);</span>  <span class="c">// make the turkey have the same interface with duck</span>
    <span class="n">fakeDuck</span><span class="p">.</span><span class="n">quack</span><span class="p">();</span>
    <span class="n">fakeDuck</span><span class="p">.</span><span class="n">fly</span><span class="p">();</span>

    <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&quot;############# The fake duck(turkey) start based on inheritance #####################&quot;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
    <span class="n">DuckAdapter_MI</span> <span class="n">fakeDuck2</span><span class="p">;</span>   <span class="c">// multi-inheritance</span>
    <span class="n">fakeDuck2</span><span class="p">.</span><span class="n">quack</span><span class="p">();</span>
    <span class="n">fakeDuck2</span><span class="p">.</span><span class="n">fly</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td></tr></table></div>
<p>输出结果为:</p>
<div class="highlight-python"><pre>############# The real duck start #####################
Quack
I'm flying
############# The real turkey start #####################
Gobble gobble
I'm flying a short distance.
############# The fake duck(turkey) start based on composition #####################
Gobble gobble
I'm flying a short distance.
I'm flying a short distance.
I'm flying a short distance.
I'm flying a short distance.
I'm flying a short distance.
############# The fake duck(turkey) start based on inheritance #####################
Gobble gobble
I'm flying a short distance.
I'm flying a short distance.
I'm flying a short distance.
I'm flying a short distance.
I'm flying a short distance.</pre>
</div>
</div>
<div class="section" id="id5">
<h2>面对新的需要<a class="headerlink" href="#id5" title="永久链接至标题">¶</a></h2>
<p>对于新的需要,如鸡要适配鸭的接口,只需:</p>
<ul class="simple">
<li>使用 <strong>基于组合</strong> 或者 <strong>基于继承</strong> 的方式来实现鸭适配器的接口</li>
<li>实现鸭相应的函数</li>
</ul>
</div>
<div class="section" id="id6">
<h2>更多注意<a class="headerlink" href="#id6" title="永久链接至标题">¶</a></h2>
<p><a class="reference external" href="decorator.html#id1"><em>Decorator模式</em></a> 与 <a class="reference internal" href="#id1"><em>Adapter模式</em></a> 及 <a class="reference external" href="facade.html#id1"><em>Facade模式</em></a> 区别:</p>
<ul class="simple">
<li>Decorator模式 <strong>不改变接口,但是增加了责任(功能)</strong></li>
<li>Adapter模式 <strong>将一个接口转换为另一个</strong></li>
<li>Facade模式 <strong>使得一个接口更加简单</strong></li>
</ul>
</div>
<div class="section" id="id7">
<h2>参考更多<a class="headerlink" href="#id7" title="永久链接至标题">¶</a></h2>
<ol class="arabic simple">
<li><a class="reference external" href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter_Pattern</a></li>
</ol>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">內容目录</a></h3>
            <ul>
<li><a class="reference external" href="">adapter模式</a><ul>
<li><a class="reference external" href="#id1">adapter模式定义</a></li>
<li><a class="reference external" href="#id3">问题描述</a></li>
<li><a class="reference external" href="#id4">具体代码</a></li>
<li><a class="reference external" href="#id5">面对新的需要</a></li>
<li><a class="reference external" href="#id6">更多注意</a></li>
<li><a class="reference external" href="#id7">参考更多</a></li>
</ul>
</li>
</ul>

            <h4>上一个主题</h4>
            <p class="topless"><a href="command.html"
                                  title="上一章">command模式</a></p>
            <h4>下一个主题</h4>
            <p class="topless"><a href="facade.html"
                                  title="下一章">facade模式</a></p>
            <h3>本页</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/designpattern/adapter.txt"
                     rel="nofollow">显示源代码</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>快速搜索</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="搜索" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              输入相关的模块，术语，类或者函数名称进行搜索
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="facade.html" title="facade模式"
             >下一页</a> |</li>
        <li class="right" >
          <a href="command.html" title="command模式"
             >上一页</a> |</li>
        <li><a href="../index.html">programming v0.1 documentation</a> &raquo;</li>
          <li><a href="index.html" >设计模式学习笔记</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; 版权所有 2009, zhutao.iscas@gmail.com.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>